//
//  CREmoticonManager.m
//  TestPrj
//
//  Created by 朱铅峰 on 2020/8/4.
//  Copyright © 2020 Xing. All rights reserved.
//

#import "CREmoticonManager.h"
#import "CRInputEmoticonDefine.h"
#import "CRKit.h"
#import "UIImage+CRKit.h"
#import "NSBundle+CRKit.h"

@implementation CRInputEmoticon

- (CREmoticonType)type {
    if (_unicode.length) {
        return CREmoticonTypeUnicode;
    } else {
        return CREmoticonTypeFile;
    }
}

@end

@implementation CRInputEmoticonCatalog
@end

@implementation CRInputEmoticonLayout

- (id)initEmojiLayout:(CGFloat)width {
    
    self = [super init];
    if (self) {
        _rows            = CRKit_EmojRows;
        _columes         = ((width - CRKit_EmojiLeftMargin - CRKit_EmojiRightMargin) / CRKit_EmojImageWidth);
        _itemCountInPage = _rows * _columes -1;
        _cellWidth       = (width - CRKit_EmojiLeftMargin - CRKit_EmojiRightMargin) / _columes;
        _cellHeight      = CRKit_EmojCellHeight;
        _imageWidth      = CRKit_EmojImageWidth;
        _imageHeight     = CRKit_EmojImageHeight;
        _emoji           = YES;
    }
    return self;
}

- (id)initCharletLayout:(CGFloat)width {
    
    self = [super init];
    if (self) {
        _rows            = CRKit_PicRows;
        _columes         = ((width - CRKit_EmojiLeftMargin - CRKit_EmojiRightMargin) / CRKit_PicImageWidth);
        _itemCountInPage = _rows * _columes;
        _cellWidth       = (width - CRKit_EmojiLeftMargin - CRKit_EmojiRightMargin) / _columes;
        _cellHeight      = CRKit_PicCellHeight;
        _imageWidth      = CRKit_PicImageWidth;
        _imageHeight     = CRKit_PicImageHeight;
        _emoji           = NO;
    }
    return self;
}

@end

@interface CRInputEmoticonManager ()
@property (nonatomic,strong) NSArray *catalogs;
@end

@implementation CRInputEmoticonManager

+ (instancetype)sharedManager {
    
    static CRInputEmoticonManager *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[CRInputEmoticonManager alloc]init];
    });
    return instance;
}

- (id)init {
    
    if (self = [super init]) {
        [self parsePlist];
        [self preloadEmoticonResource];
    }
    return self;
}

- (void)start {};

- (CRInputEmoticonCatalog *)emoticonCatalog:(NSString *)catalogID {
    
    for (CRInputEmoticonCatalog *catalog in _catalogs) {
        if ([catalog.catalogID isEqualToString:catalogID]) {
            return catalog;
        }
    }
    return nil;
}


- (CRInputEmoticon *)emoticonByTag:(NSString *)tag {
    
    CRInputEmoticon *emoticon = nil;
    if ([tag length]) {
        for (CRInputEmoticonCatalog *catalog in _catalogs) {
            emoticon = [catalog.tag2Emoticons objectForKey:tag];
            if (emoticon) {
                break;
            }
        }
    }
    return emoticon;
}


- (CRInputEmoticon *)emoticonByID:(NSString *)emoticonID {
    
    CRInputEmoticon *emoticon = nil;
    if ([emoticonID length]) {
        for (CRInputEmoticonCatalog *catalog in _catalogs) {
            emoticon = [catalog.id2Emoticons objectForKey:emoticonID];
            if (emoticon) {
                break;
            }
        }
    }
    return emoticon;
}

- (CRInputEmoticon *)emoticonByCatalogID:(NSString *)catalogID
                           emoticonID:(NSString *)emoticonID {
    CRInputEmoticon *emoticon = nil;
    if ([emoticonID length] && [catalogID length]) {
        for (CRInputEmoticonCatalog *catalog in _catalogs) {
            if ([catalog.catalogID isEqualToString:catalogID]) {
                emoticon = [catalog.id2Emoticons objectForKey:emoticonID];
                break;
            }
        }
    }
    return emoticon;
}

- (void)parsePlist {
    
    NSMutableArray *catalogs = [NSMutableArray array];
    NSString *filepath = [NSBundle nim_EmojiPlistFile];
    if (filepath) {
        NSArray *array = [NSArray arrayWithContentsOfFile:filepath];
        for (NSDictionary *dict in array)
        {
            NSDictionary *info = dict[@"info"];
            NSArray *emoticons = dict[@"data"];
            
            CRInputEmoticonCatalog *catalog = [self catalogByInfo:info
                                                     emoticons:emoticons];
            [catalogs addObject:catalog];
        }
    }
    _catalogs = catalogs;
}

- (CRInputEmoticonCatalog *)catalogByInfo:(NSDictionary *)info
                             emoticons:(NSArray *)emoticonsArray {
    
    CRInputEmoticonCatalog *catalog = [[CRInputEmoticonCatalog alloc]init];
    catalog.catalogID = info[@"id"];
    catalog.title     = info[@"title"];
    catalog.icon      = info[@"normal"];
    catalog.iconPressed = info[@"pressed"];
    NSMutableDictionary *tag2Emoticons = [NSMutableDictionary dictionary];
    NSMutableDictionary *id2Emoticons = [NSMutableDictionary dictionary];
    NSMutableArray *emoticons = [NSMutableArray array];
    
    for (NSDictionary *emoticonDict in emoticonsArray) {
        CRInputEmoticon *emoticon  = [[CRInputEmoticon alloc] init];
        emoticon.emoticonID     = emoticonDict[@"id"];
        emoticon.tag            = emoticonDict[@"tag"];
        emoticon.unicode        = emoticonDict[@"unicode"];
        emoticon.filename       = emoticonDict[@"file"];
        
        if (emoticon.emoticonID) {
            [emoticons addObject:emoticon];
            id2Emoticons[emoticon.emoticonID] = emoticon;
        }
        if (emoticon.tag) {
            tag2Emoticons[emoticon.tag] = emoticon;
        }
    }
    
    catalog.emoticons       = emoticons;
    catalog.id2Emoticons    = id2Emoticons;
    catalog.tag2Emoticons   = tag2Emoticons;
    return catalog;
}

- (void)preloadEmoticonResource {
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        for (CRInputEmoticonCatalog *catalog in self.catalogs) {
            [catalog.emoticons enumerateObjectsUsingBlock:^(CRInputEmoticon  *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if (obj.filename) {
                   __unused UIImage *image = [UIImage cr_emoticonInKit:obj.filename];
                }
            }];
        }
    });
}
@end
